/* 
    工具类
*/

//格式化时间
export function formatDate(val,type = 0){
    let date = val === undefined ? new Date() : new Date(val);
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let day = date.getDate();
    let h = date.getHours();
    let m = date.getMinutes();
    let s = date.getSeconds();
    if(type === 0){
        return year + "-" + addZero(month) + "-" + addZero(day) + " " + addZero(h) + ":" + addZero(m) + ":" + addZero(s);
    } else if (type === 1){
        return year + "-" + addZero(month) + "-" + addZero(day);
    } else if (type === 2){
        return addZero(h) + ":" + addZero(m) + ":" + addZero(s);
    } else if (type === 3){
        return year + "-" + addZero(month) + "-" + addZero(day) + " 00:00:00";
    }
}

function addZero(val){
    return val > 9 ? val : "0" + val;
}


//解析扫码规则
 export function analysisScanCodeRules(scanCode){
    const code = scanCode || "";
    const analysisFlag = "/";
    const analysisResult = {};
    console.log("要解析的码:" , code);
    if(code === ""){
        return;
    }
    const splitCode = code.split(analysisFlag);
    const materialInfo = splitCode[0].trim().replace('\n','');
    const batchInfo = splitCode[1].trim().replace('\n','');
    const numInfo = splitCode[2].trim().replace('\n','');
    analysisResult.materialInfo = materialInfo;
    analysisResult.batchInfo = batchInfo;
    analysisResult.numInfo = numInfo;
    console.log("解析结果:",analysisResult);
    return analysisResult;
}